home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue72 / dynimag / DrBobCGI.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-07-04  |  5.9 KB  |  213 lines

  1. unit DrBobCGI;
  2. {===================================================================}
  3. { DrBobCGI (c) 1999-2001 by Bob Swart (aka Dr.Bob - www.drbob42.com }
  4. { version 1.0 - obtain standard CGI variable values by "value()".   }
  5. { version 2.0 - obtain CGI values, cookies and IP/UserAgent values. }
  6. { version 2.1 - obtain Authorisation values (base64-encoded string) }
  7. { version 3.0 - ported to Kylix 1.0, still works with Delphi 4+ too }
  8. {               Note: DrBobCGI does not work with Delphi 3 or lower }
  9. { version 3.1 - combining GET and POST fields inside one Data field }
  10. {===================================================================}
  11. {$IFDEF WIN32}
  12.   {$IFNDEF MSWINDOWS}
  13.     {$DEFINE MSWINDOWS}
  14.   {$ENDIF}
  15. {$ENDIF}
  16. interface
  17. type
  18.   TRequestMethod = (Unknown,Get,Post);
  19. var
  20.   RequestMethod: TRequestMethod = Unknown;
  21.  
  22. var
  23.   ContentLength: Integer = 0;
  24.   RemoteAddress: String[16] = ''; { IP }
  25.   HttpUserAgent: String[128] = ''; { Browser, OS }
  26.   Authorization: String[255] = ''; { Authorization }
  27.   ScriptName: String[128] = ''; { scriptname URL }
  28.  
  29.   function Value(const Field: ShortString; Convert: Boolean = True): ShortString;
  30.   function CookieValue(const Field: ShortString): ShortString;
  31.  
  32. implementation
  33. uses
  34.   {$IFDEF MSWINDOWS}
  35.     Windows,
  36.   {$ENDIF}
  37.   {$IFDEF LINUX}
  38.     Libc,
  39.   {$ENDIF}
  40.     SysUtils;
  41.  
  42.   function _Value(const Field: ShortString;
  43.                   const Data: AnsiString; Sep: Char = '&';
  44.                   Convert: Boolean = True): ShortString;
  45.   { 1998/01/02: check for complete match of Field name }
  46.   { 1999/03/01: do conversion *after* searching fields }
  47.   var
  48.     i: Integer;
  49.     Str: String[3];
  50.     len: Byte absolute Result;
  51.   begin
  52.     len := 0; { Result := '' }
  53.     i := Pos(Sep+Field+'=',Data);
  54.     if i = 0 then
  55.     begin
  56.       i := Pos(Field+'=',Data);
  57.       if i > 1 then i := 0
  58.     end
  59.     else Inc(i); { skip '&' }
  60.     if i > 0 then
  61.     begin
  62.       Inc(i,Length(Field)+1);
  63.       while Data[i] <> Sep do
  64.       begin
  65.         Inc(len);
  66.         if (Data[i] = '%') and Convert then // special code
  67.         begin
  68.           Str := '$00';
  69.           Str[2] := Data[i+1];
  70.           Str[3] := Data[i+2];
  71.           Inc(i,2);
  72.           Result[len] := Chr(StrToInt(Str))
  73.         end
  74.         else
  75.           if (Data[i] = ' ') and not Convert then Result[len] := '+'
  76.           else
  77.             Result[len] := Data[i];
  78.         Inc(i)
  79.       end
  80.     end
  81.     else Result := '$' { no javascript }
  82.   end {_Value};
  83.  
  84. var
  85.   Data: AnsiString = '';
  86.  
  87.   function Value(const Field: ShortString; Convert: Boolean = True): ShortString;
  88.   begin
  89.     Result := _Value(Field, Data, '&', Convert)
  90.   end;
  91.  
  92. var
  93.   Cookie: ShortString;
  94.  
  95.   function CookieValue(const Field: ShortString): ShortString;
  96.   begin
  97.     Result := _Value(Field, Cookie, ';');
  98.     if Result = '$' then Result := Cookie { debug }
  99.   end;
  100.  
  101. var
  102.   P: PChar;
  103.   StartData,i: Integer;
  104. {$IFDEF MSWINDOWS}
  105.   Str: ShortString;
  106. {$ENDIF}
  107.  
  108. initialization
  109. {$IFDEF MSWINDOWS}
  110. // Tested on IIS and PWS
  111.   P := GetEnvironmentStrings;
  112.   while P^ <> #0 do
  113.   begin
  114.     Str := StrPas(P);
  115.     if Pos('REQUEST_METHOD=',Str) > 0 then
  116.     begin
  117.       Delete(Str,1,Pos('=',Str));
  118.       if Str = 'POST' then RequestMethod := Post
  119.       else
  120.         if Str = 'GET' then RequestMethod := Get
  121.     end;
  122.     if Pos('CONTENT_LENGTH=',Str) = 1 then
  123.     begin
  124.       Delete(Str,1,Pos('=',Str));
  125.       ContentLength := StrToInt(Str)
  126.     end;
  127.     if Pos({HTTP_}'QUERY_STRING=',Str) > 0 then
  128.     begin
  129.       Delete(Str,1,Pos('=',Str));
  130. //    SetLength(Data,Length(Str));
  131.       Data := Str + '&'
  132.     end;
  133.     if Pos({HTTP_}'COOKIE=',Str) > 0 then // TDM #45
  134.     begin
  135.       Delete(Str,1,Pos('=',Str));
  136. //    SetLength(Cookie,Length(Str));
  137.       Cookie := Str + ';'
  138.     end
  139.     else
  140.     if Pos({HTTP_}'REMOTE_ADDR',Str) > 0 then // TDM #39
  141.     begin
  142.       Delete(Str,1,Pos('=',Str));
  143.       RemoteAddress := Str
  144.     end
  145.     else
  146.     if Pos({HTTP_}'USER_AGENT',Str) > 0 then // TDM #39
  147.     begin
  148.       Delete(Str,1,Pos('=',Str));
  149.       if Pos(')',Str) > 0 then
  150.         Delete(Str,Pos(')',Str)+1,Length(Str)); {!!}
  151.       HttpUserAgent := Str
  152.     end
  153.     else
  154.     if (Pos({HTTP_}'AUTHORIZATION',Str) > 0) then // TDM #55
  155.     begin
  156.       Delete(Str,1,Pos('=',Str));
  157.       Authorization := Str;
  158.     end
  159.     else
  160.     if Pos({HTTP_}'SCRIPT_NAME',Str) > 0 then // TDM #71
  161.     begin
  162.       Delete(Str,1,Pos('=',Str));
  163.       ScriptName := Str
  164.     end;
  165.     Inc(P, StrLen(P)+1)
  166.   end;
  167. {$ENDIF}
  168. {$IFDEF LINUX}
  169. // Tested on Apache for Linux
  170.   P := getenv('REQUEST_METHOD');
  171.   if P = 'POST' then RequestMethod := Post
  172.   else
  173.     if P = 'GET' then RequestMethod := Get;
  174.   ContentLength := StrToIntDef(getenv('CONTENT_LENGTH'),0);
  175.   Data := getenv('HTTP_QUERY_STRING');
  176.   if Data = '' then
  177.     Data := getenv('QUERY_STRING');
  178.   if Data <> '' then Data := Data + '&';
  179.   Cookie := StrPas(getenv('HTTP_COOKIE'));
  180.   if Cookie = '' then
  181.     Cookie := StrPas(getenv('COOKIE'));
  182.   RemoteAddress := StrPas(getenv('HTTP_REMOTE_ADDR'));
  183.   if RemoteAddress = '' then
  184.     RemoteAddress := StrPas(getenv('REMOTE_ADDR'));
  185.   HttpUserAgent := StrPas(getenv('HTTP_USER_AGENT'));
  186.   if HttpUserAgent = '' then
  187.     HttpUserAgent := StrPas(getenv('USER_AGENT'));
  188.   Authorization := StrPas(getenv('HTTP_AUTHORIZATION'));
  189.   if Authorization = '' then
  190.     Authorization := StrPas(getenv('AUTHORIZATION'));
  191.   ScriptName := StrPas(getenv('SCRIPT_NAME'));
  192. {$ENDIF}
  193.   if RequestMethod = Post then
  194.   begin
  195.     StartData := Length(Data);
  196.     SetLength(Data,StartData+ContentLength+1);
  197.     for i:=1 to ContentLength do read(Data[StartData+i]);
  198.     Data[StartData+ContentLength+1] := '&';
  199.   { if IOResult <> 0 then { skip }
  200.   end;
  201.   i := 0;
  202.   while i < Length(Data) do
  203.   begin
  204.     Inc(i);
  205.     if Data[i] = '+' then Data[i] := ' '
  206.   end;
  207.   if i > 0 then Data[i+1] := '&'
  208.            else Data := '&';
  209. finalization
  210.   Cookie := '';
  211.   Data := ''
  212. end.
  213.